package com.shujia.mapredue;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;

public class Demo8Join {

    public static class JoinMap extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            //获取输入数据的路径
            FileSplit inputSplit = (FileSplit) context.getInputSplit();
            String path = inputSplit.getPath().toString();

            if (path.contains("student")) {

                //以学号作为key 数据到reduce端
                String id = value.toString().split(",")[0];

                String s = value.toString();
                //将数据前面加上一个标记，在reduce端通过标记识别数据
                String line = "#" + s;
                context.write(new Text(id), new Text(line));

            } else if (path.contains("score")) {
                //以学号作为key 数据到reduce端
                String id = value.toString().split(",")[0];

                String s = value.toString();
                //将数据前面加上一个标记，在reduce端通过标记识别数据
                String line = "*" + s;
                context.write(new Text(id), new Text(line));

            }
        }
    }

    public static class JoinReduce extends Reducer<Text, Text, Text, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {


            ArrayList<String> scores = new ArrayList<String>();
            String stuInfo = "";

            //通过标记取出学生数据和分数数据
            for (Text value : values) {
                String line = value.toString();

                if (line.startsWith("#")) {
                    stuInfo = line.substring(1);
                } else if (line.startsWith("*")) {
                    scores.add(line.substring(1));
                }
            }

            //关连学生数据和分数数据，输出的hdfs
            for (String score : scores) {
                String line = score + "," + stuInfo;

                context.write(new Text(line), NullWritable.get());
            }


        }
    }


    public static void main(String[] args) throws Exception {

        Job job = Job.getInstance();

        job.setJobName("join");

        job.setJarByClass(Demo8Join.class);


        //指定学生表的路径
        FileInputFormat.addInputPath(job, new Path("/data/student"));

        //指定分数表的路径
        FileInputFormat.addInputPath(job, new Path("/data/score"));


        job.setMapperClass(JoinMap.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);


        job.setReducerClass(JoinReduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);


        //输出路径
        FileOutputFormat.setOutputPath(job, new Path("/data/jon"));

        job.waitForCompletion(true);


    }
}
